package com.dk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dk.data.Result;
import com.dk.data.ResultEnum;
import com.dk.pojo.content.TbContent;
import com.dk.service.content.ContentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("content")
public class ContentController {

    @Reference
    private ContentService contentService;

    private final Logger logger = LoggerFactory.getLogger(ContentController.class);

    @RequestMapping("listContent")
    @ResponseBody
    public Result listContent() {
        try {
            List<TbContent> contents = contentService.listContent();
            return Result.response(ResultEnum.OK).setData(contents);
        }catch (Exception e) {
            logger.error("获取广告列表错误",e);
            return Result.response(ResultEnum.FAIL);
        }
    }

    @RequestMapping("saveContent")
    @ResponseBody
    public Result saveContent(TbContent content) {
        try {
            int i = contentService.saveContent(content);
            return Result.response(ResultEnum.OK);
        } catch (Exception e) {
            logger.error("保存广告错误",e);
            return Result.response(ResultEnum.FAIL);
        }
    }

    @RequestMapping("contentById")
    @ResponseBody
    public Result contentById(Long id) {
        try {
            TbContent content = contentService.contentById(id);
            return Result.response(ResultEnum.OK).setData(content);
        } catch (Exception e) {
            logger.error("获取广告错误",e);
            return Result.response(ResultEnum.FAIL);
        }
    }

    @RequestMapping("updateContent")
    @ResponseBody
    public Result updateContent(TbContent content) {
        try {
            int i = contentService.updateContent(content);
            return Result.response(ResultEnum.OK);
        } catch (Exception e) {
            logger.error("修改广告错误",e);
            return Result.response(ResultEnum.FAIL);
        }
    }
}
